package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private FactoryDao factoryDao;
    @Override
    public void save(ContractProduct contractProduct) {
        //设置总金额
        double acount=0;
        if(contractProduct.getCnumber()!=null&&contractProduct.getPrice()!=null) {
            acount = contractProduct.getCnumber() * contractProduct.getPrice();
        }
            contractProduct.setAmount(acount);
            contractProductDao.insertSelective(contractProduct);
        //修改合同的总金额和货物数量
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()+acount);
        contract.setProNum(contract.getProNum()+1);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        ContractProduct contractProduct1 = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        //原来的小计
        Double amount = contractProduct1.getAmount();
        //修改后的价格
        double amount_now=0;
        if(contractProduct.getCnumber()!=null&&contractProduct.getPrice()!=null) {
            amount_now = contractProduct.getPrice() * contractProduct.getCnumber();
        }
        contractProduct.setAmount(amount_now);
        //更新货物的价格
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        //原来合同对象
        Contract contract = contractDao.selectByPrimaryKey(contractProduct1.getContractId());
        //原来的总价格
        Double totalAmount = contract.getTotalAmount();
        //新计算的价格
        Double d=totalAmount-amount+amount_now;
        contract.setTotalAmount(d);
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void delete(String id) {
        //获取原来的货物订单对象
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //查询此货物的所有附件
        ExtCproductExample example  =  new ExtCproductExample();
        ExtCproductExample.Criteria criteria = example.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> exts = extCproductDao.selectByExample(example);
        //被删货物的总金额
        Double amount = contractProduct.getAmount();
        //附件的总金额
        for (ExtCproduct ext : exts) {
            amount+=ext.getAmount();
            //删除每个附件
            extCproductDao.deleteByPrimaryKey(ext.getId());
        }
        //删除货物
        contractProductDao.deleteByPrimaryKey(id);
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setProNum(contract.getProNum()-1);
        contract.setExtNum(exts.size());
        contract.setTotalAmount(contract.getTotalAmount()-amount);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list,5);
    }

    @Override
    public void saveList(List<ContractProduct> list) {
        for (ContractProduct contractProduct : list) {
            FactoryExample factoryExample = new FactoryExample();
            factoryExample.createCriteria().andCtypeEqualTo("货物").andFactoryNameEqualTo(contractProduct.getFactoryName());
            List<Factory> list1 = factoryDao.selectByExample(factoryExample);
            if(list1!=null&&list1.size()>0) {
                String id = list1.get(0).getId();
                contractProduct.setFactoryId(id);
            }
            save(contractProduct);
        }
    }
}
